//
//  ST_RectangleST_LoveModuleController.swift
//  btvideo
//
//  Created by mac on 2020/9/19.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_RectangleST_LoveModuleController: UIViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
         
        
        view.addSubview(segment_control)
        segment_control.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(ST_MeetYouSegmentControl.height)
        }
        segment_control.selected_closure = {[weak self] idx in
            self?.scrollView.setContentOffset(CGPoint.init(x: ScreenWidth*idx.cgFloat, y: 0), animated: true)
        }
        
        
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(segment_control.snp.bottom)
        }
        
        scrollView.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalToSuperview().dividedBy(0.5)
            make.height.equalToSuperview()
        }
        
        self.addChild(love_me_controller)
        contentView.addSubview(love_me_controller.view)
        love_me_controller.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.left.top.bottom.equalToSuperview()
        }
        
        
        self.addChild(my_love_controller)
        contentView.addSubview(my_love_controller.view)
        my_love_controller.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.right.top.bottom.equalToSuperview()
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(red_point_observer), name: Notification.Name.love_me_red_point, object: nil)
    }
    
    @objc fileprivate func red_point_observer(){
        
        segment_control.show_red_point(index: 0)
    }
    
    func hidden_red_point(){
        segment_control.hidden_red_point(index: 0)
    }
    
//    MARK: - Properties
    fileprivate lazy var scrollView:UIScrollView = {
       let control = UIScrollView()
        control.delegate = self
        control.isPagingEnabled = true
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        
        return control
    }()
    
    fileprivate lazy var contentView:UIView = {
       let control = UIView()
        return control
    }()
    
    
    fileprivate lazy var love_me_controller:ST_RectangleLoveMeController = {
       let control = ST_RectangleLoveMeController()
        return control
    }()
    
    lazy var my_love_controller:ST_RectangleMyLoveController = {
       let control = ST_RectangleMyLoveController()
        return control
    }()
    
    
    fileprivate lazy var segment_control:ST_MeetYouSegmentControl = {
       let control = ST_MeetYouSegmentControl()
        control.titleArray = ["喜欢了我","我喜欢"]
        return control
    }()
    
    func filter_refresh(){
        
        love_me_controller.refresh_data()
        my_love_controller.refresh_data()
        
    }
}


extension ST_RectangleST_LoveModuleController:UIScrollViewDelegate{
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
                
        
        if !decelerate{
                scrollviewdidEndScroll(scrollView)
        }
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
            scrollviewdidEndScroll(scrollView)
        
    }
    
    
//    MARK: 停止滚动
    func scrollviewdidEndScroll(_ scrollView:UIScrollView){
        
        let currPage = (scrollView.contentOffset.x/ScreenWidth).int
        
        self.segment_control.selectBtn(index: currPage)
        
    }
    
}
